package com.xiaoxing.zhinenghuacangku_jining.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.xiaoxing.zhinenghuacangku_jining.R;
import com.xiaoxing.zhinenghuacangku_jining.R2;
import com.xiaoxing.zhinenghuacangku_jining.di.component.DaggerJiNing_RuKuQingZhangComponent;
import com.xiaoxing.zhinenghuacangku_jining.di.module.JiNing_RuKuQingZhangModule;
import com.xiaoxing.zhinenghuacangku_jining.mvp.contract.JiNing_RuKuQingZhangContract;
import com.xiaoxing.zhinenghuacangku_jining.mvp.model.entity.JiNing_GetUserSpaceListForAutoStore;
import com.xiaoxing.zhinenghuacangku_jining.mvp.presenter.JiNing_RuKuQingZhangPresenter;
import com.xiaoxing.zhinenghuacangku_jining.mvp.ui.entity.JiNing_RuKuQingZhang;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonres.view.NiceSpinner;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.base.BaseConstants.SCAN_REQUEST_CODE;
import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.xiaoxing.zhinenghuacangku_jining.mvp.ui.activity.JiNing_ReturnLibraryActivity.REQUEST_HUAN_CHONG_WEI;
import static com.xiaoxing.zhinenghuacangku_jining.mvp.ui.activity.JiNing_ReturnLibraryActivity.REQUEST_WU_LIAO;


@Route(path = RouterHub.ZHI_NENG_HUA_CANG_KU_JINING_ACTIVITY_RU_KU_QING_ZHANG)
public class JiNing_RuKuQingZhangActivity extends BaseActivity<JiNing_RuKuQingZhangPresenter> implements JiNing_RuKuQingZhangContract.View {


    private static final String RU_KU_QING_ZHANG_MID = "ru_ku_qing_zhang_mid";
    private static Disposable mDisposable;
    @BindView(R2.id.btn_scan_wu_liao)
    Button mBtnScanWuLiao;
    @BindView(R2.id.ll_cang_wei)
    LinearLayout mLlCangWei;
    @BindView(R2.id.btn_chong_xin_ru_ku)
    Button mBtnChongXinRuKu;
    @BindView(R2.id.et_weizhi)
    EditText etWeizhi;
    @BindView(R2.id.tv_check)
    TextView tvCheck;
    @BindView(R2.id.nice_position)
    NiceSpinner spPosition;
    @BindView(R2.id.et_num)
    EditText et_num;
    @BindView(R2.id.btn_sao_miao_huan_chong_wei)
    Button btnSaoMiaoHuanChongWeiZhi;
    private String mid = "";
    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;
    private String instoreBufferId = "";
    /**
     * 初始化缓冲位列表下拉选
     */
    private int spid = 0;
    private String spname = "";
    private String cangWeiPosition = "";
    private String mResultCode = "";//为3 的时候执行InstoreCallAGV
    private double num = 0;
    private TextWatcher numTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            String etNum = et_num.getText().toString();
            if (!TextUtils.isEmpty(etNum) && Double.parseDouble(etNum) > 0) {
                if (Double.parseDouble(etNum) > num) {
                    et_num.setText(num + "");
                    ArmsUtils.makeText(getContext(), "数量不能大于" + num);
                    return;
                }
//                num = Double.parseDouble(et_num.getText().toString());
            }


        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerJiNing_RuKuQingZhangComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .jiNing_RuKuQingZhangModule(new JiNing_RuKuQingZhangModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.zhi_neng_cang_ku_ji_ning_activity_ru_ku_qing_zhang; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.zhi_neng_cang_ku_lin_yi_activity_ru_ku_qing_zhang));
//        getRuKuQingZhangData();
        mSharedPreferencesHelper.putString(RU_KU_QING_ZHANG_MID, "");
        mBtnScanWuLiao.setEnabled(true);
        mBtnChongXinRuKu.setEnabled(false);
        mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
        if (!TextUtils.isEmpty(mSharedPreferencesHelper.getString(RU_KU_QING_ZHANG_MID))) {
            mid = mSharedPreferencesHelper.getString(RU_KU_QING_ZHANG_MID);
            mBtnScanWuLiao.setEnabled(false);
            mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
        }


//        intervalTransfered();


    }

    private void getRuKuQingZhangData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getRuKuQingZhangData(map);
    }

    @Override
    public void getRuKuQingZhangDataSuccess(JiNing_RuKuQingZhang entityData) {


    }

    @Override
    public void callAgvGetMaterialToInstoreBufferSuccess(String entityData) {

        //  1;//命令发送完成
        //-1;//入库区不可用
        // 0;//操作不合法

        LogUtils.debugEInfo("entityDataCOde======" + entityData);

        if (entityData.equals("1")) {
            mSharedPreferencesHelper.putString(RU_KU_QING_ZHANG_MID, mid);
            mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
            mBtnScanWuLiao.setEnabled(false);
            mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
            mBtnChongXinRuKu.setEnabled(false);
            ArmsUtils.makeText(getContext(), "命令发送完成");
            intervalTransfered();

        } else if (entityData.equals("2")) {
            mSharedPreferencesHelper.putString(RU_KU_QING_ZHANG_MID, mid);

            mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
            mBtnScanWuLiao.setEnabled(false);
            mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
            mBtnChongXinRuKu.setEnabled(true);
        } else if (entityData.contains("3@")) {
            String[] str = entityData.split("@");

            if (str.length > 0) {
                mSharedPreferencesHelper.putString(RU_KU_QING_ZHANG_MID, mid);
                mResultCode = str[0];
                et_num.setText(str[1]);
                num = Double.parseDouble(str[1]);
                mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                mBtnScanWuLiao.setEnabled(false);
                mLlCangWei.setVisibility(View.VISIBLE);

                et_num.addTextChangedListener(numTextWatcher);

            }

        } else if (entityData.equals("-1")) {
            ArmsUtils.makeText(getContext(), "入库区不可用");
        } else if (entityData.equals("0")) {
            ArmsUtils.makeText(getContext(), "操作不合法");
        }
    }

    private void intervalTransfered() {

        Observable.interval(30, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                        mDisposable = disposable;
                    }

                    @Override
                    public void onNext(@NonNull Long number) {
//                        if (!TextUtils.isEmpty(mSharedPreferencesHelper.getString(RU_KU_QING_ZHANG_MID)) && mPresenter != null)
//                            mPresenter.checkCanReInstore(RuKuQingZhangActivity.this, mSharedPreferencesHelper.getString(RU_KU_QING_ZHANG_MID));
                        checkCanReInstore(mSharedPreferencesHelper.getString(RU_KU_QING_ZHANG_MID));
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    private void checkCanReInstore(String mid) {

        showLoading();
        String methodName = "CheckCanReInstore";
        SoapParams params = new SoapParams();
        params.put("mid", mid);
        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    if (result.equals("true")) {
                        mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
                        mBtnChongXinRuKu.setEnabled(true);
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog();

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void checkCanReInstoreSuccess(String entityData) {
        if (entityData.equals("true")) {
            mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
            mBtnChongXinRuKu.setEnabled(true);
        }
    }

    @Override
    public void instoreAgainSuccess(String entityData) {
        if (!entityData.equals("0")) {
            mid = "";
            mSharedPreferencesHelper.putString(RU_KU_QING_ZHANG_MID, "");
            mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
            mBtnScanWuLiao.setEnabled(true);
            mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
            mBtnChongXinRuKu.setEnabled(false);
            ArmsUtils.makeText(getContext(), "命令发送完成");
        } else {//为0时
            ArmsUtils.makeText(getContext(), "发送失败");
        }
    }

    @Override
    public void getUserSpaceListForAutoStoreSuccess(List<JiNing_GetUserSpaceListForAutoStore> getUserSpaceListForAutoStoreList) {
        initSpPosition(getUserSpaceListForAutoStoreList);

    }

    private void initSpPosition(List<JiNing_GetUserSpaceListForAutoStore> getUserSpaceListForAutoStoreList) {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < getUserSpaceListForAutoStoreList.size(); i++) {
            list.add(String.valueOf(
                    new StringBuilder()
                            .append(getUserSpaceListForAutoStoreList.get(i).getSpname())
            ));
        }
        spid = getUserSpaceListForAutoStoreList.get(0).getSpid();
        spname = list.get(0);
        cangWeiPosition = String.valueOf(getUserSpaceListForAutoStoreList.get(0).getSpid());
        spPosition.attachDataSource(list);
        spPosition.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cangWeiPosition = String.valueOf(getUserSpaceListForAutoStoreList.get(position).getSpid());
                spid = getUserSpaceListForAutoStoreList.get(position).getSpid();
                spname = getUserSpaceListForAutoStoreList.get(position).getSpname();
            }
        });
    }

    @Override
    public void addAgvTaskSuccess(String addAgvTask) {
        mResultCode = "";
        et_num.setText("");
        etWeizhi.setText("");
        mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
        mBtnScanWuLiao.setEnabled(true);
        mLlCangWei.setVisibility(View.GONE);
        mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
        mBtnChongXinRuKu.setEnabled(false);
    }

    @Override
    public void setInstoreBufferReadyForAGVSuccess(String setInstoreBufferReadyForAGV) {
        if (setInstoreBufferReadyForAGV.equals("false")) {

//            ArmsUtils.makeText(this, "1.此缓冲位已占满 2.此缓冲位没有启用 3.此缓冲位是出库缓冲位(标签贴错)");
            ArmsUtils.makeText(this, "缓冲位置不可用，请联系管理员");

        } else {
            mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
            mBtnChongXinRuKu.setEnabled(true);
            btnSaoMiaoHuanChongWeiZhi.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
            btnSaoMiaoHuanChongWeiZhi.setEnabled(false);
        }
    }

    @OnClick(R2.id.tv_check)
    public void cangWeiChaXun() {
//        if (TextUtils.isEmpty(getCangWei())) {
//            ArmsUtils.makeText(this, "请输入仓位的名称");
//            return;
//        }
        getUserSpaceListForAutoStore(getCangWei());

    }

    /**
     * 查询仓位
     *
     * @param spname
     */
    private void getUserSpaceListForAutoStore(String spname) {
//        mPresenter.getUserSpaceListForAutoStore(this, mSharedPreferencesHelper.getString(BaseConstants.UID), spname);
        getUserSpaceListForAutoStore(mSharedPreferencesHelper.getString(BaseConstants.UID), spname);
    }

    private String getCangWei() {
        return etWeizhi.getText().toString().trim();
    }

    private void getUserSpaceListForAutoStore(String user_id, String spname) {

        showLoading();
        String methodName = "GetUserSpaceListForAutoStore";
        SoapParams params = new SoapParams();
        params.put("user_id", user_id);
        params.put("spname", spname);
        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    List<JiNing_GetUserSpaceListForAutoStore> getUserSpaceListForAutoStoreList = ArmsUtils.parseString2List(result, JiNing_GetUserSpaceListForAutoStore.class);
                    initSpPosition(getUserSpaceListForAutoStoreList);

                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposable != null)
            mDisposable.dispose();
    }

    @OnClick({R2.id.btn_scan_wu_liao, R2.id.btn_chong_xin_ru_ku, R2.id.btn_sao_miao_huan_chong_wei})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_scan_wu_liao) {
            Intent newIntent = new Intent(this, JiNing_ScanQRCodeActivity.class);
            mSharedPreferencesHelper.putInt(SCAN_REQUEST_CODE, REQUEST_WU_LIAO);
            startActivityForResult(newIntent, REQUEST_WU_LIAO);
        } else if (i == R.id.btn_chong_xin_ru_ku) {


            if (!TextUtils.isEmpty(mSharedPreferencesHelper.getString(RU_KU_QING_ZHANG_MID))) {
                if (!TextUtils.isEmpty(mResultCode) && mResultCode.equals("3")) {


                    if (TextUtils.isEmpty(et_num.getText().toString())) {
                        ArmsUtils.makeText(getContext(), "请填写数量");
                        return;
                    }

//                    mPresenter.addAgvTask(this, mid, instoreBufferId, String.valueOf(spid), et_num.getText().toString(), mSharedPreferencesHelper.getString(BaseConstants.UID), "");
                    addAgvTask(mid, instoreBufferId, String.valueOf(spid), et_num.getText().toString(), mSharedPreferencesHelper.getString(BaseConstants.UID), "");


                } else {
//                    mPresenter.instoreAgain(getContext(), mSharedPreferencesHelper.getString(RU_KU_QING_ZHANG_MID));
                    instoreAgain(mSharedPreferencesHelper.getString(RU_KU_QING_ZHANG_MID));
                }
            }
        } else if (i == R.id.btn_sao_miao_huan_chong_wei) {

            if (TextUtils.isEmpty(spname)) {
                ArmsUtils.makeText(getContext(), "请先选择仓位");
                return;
            }
            if (TextUtils.isEmpty(et_num.getText().toString())) {
                ArmsUtils.makeText(getContext(), "请填写数量");
                return;
            }

            Intent newIntent = new Intent(this, JiNing_ScanQRCodeActivity.class);
            mSharedPreferencesHelper.putInt(SCAN_REQUEST_CODE, REQUEST_HUAN_CHONG_WEI);
            startActivityForResult(newIntent, REQUEST_HUAN_CHONG_WEI);
        }
    }

    private void addAgvTask(String mid, String instoreBufferId, String spid, String num, String uid, String remark) {

        showLoading();
        String methodName = "InstoreCallAGV";
        SoapParams params = new SoapParams();
        params.put("mid", mid);
        params.put("instoreBufferId", instoreBufferId);
        params.put("spid", spid);
        params.put("num", num);
        params.put("uid", uid);
        params.put("remark", remark);

        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    mResultCode = "";
                    et_num.setText("");
                    etWeizhi.setText("");
                    mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
                    mBtnScanWuLiao.setEnabled(true);
                    mLlCangWei.setVisibility(View.GONE);
                    mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                    mBtnChongXinRuKu.setEnabled(false);
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        }, getAapiUserName(), getApiPwd());
    }

    private void instoreAgain(String mid1) {
        showLoading();
        String methodName = "InstoreAgain";
        SoapParams params = new SoapParams();
        params.put("mid", mid1);

        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    if (!result.equals("0")) {
                        mid = "";
                        mSharedPreferencesHelper.putString(RU_KU_QING_ZHANG_MID, "");
                        mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
                        mBtnScanWuLiao.setEnabled(true);
                        mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                        mBtnChongXinRuKu.setEnabled(false);
                        ArmsUtils.makeText(getContext(), "命令发送完成");
                    } else {//为0时
                        ArmsUtils.makeText(getContext(), "发送失败");
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == REQUEST_WU_LIAO) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                mid = extras.getString(BaseConstants.SCAN_RESULT);
//                mPresenter.callAgvGetMaterialToInstoreBuffer(this, mid);
                callAgvGetMaterialToInstoreBuffer(mid);
            }

        } else if (resultCode == REQUEST_HUAN_CHONG_WEI) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                instoreBufferId = extras.getString(BaseConstants.SCAN_RESULT);
//                mPresenter.setInstoreBufferReadyForAGV(this, instoreBufferId, spname);
                setInstoreBufferReadyForAGV(instoreBufferId, spname);

            }
        }
    }

    private void callAgvGetMaterialToInstoreBuffer(String mid1) {
        showLoading();
        String methodName = "CallAgvGetMaterialToInstoreBuffer";
        SoapParams params = new SoapParams();
        params.put("mid", mid1);

        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    //  1;//命令发送完成
                    //-1;//入库区不可用
                    // 0;//操作不合法

                    LogUtils.debugEInfo("entityDataCOde======" + result);

                    if (result.equals("1")) {
                        mSharedPreferencesHelper.putString(RU_KU_QING_ZHANG_MID, mid);
                        mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                        mBtnScanWuLiao.setEnabled(false);
                        mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                        mBtnChongXinRuKu.setEnabled(false);
                        ArmsUtils.makeText(getContext(), "命令发送完成");
                        intervalTransfered();

                    } else if (result.equals("2")) {
                        mSharedPreferencesHelper.putString(RU_KU_QING_ZHANG_MID, mid);

                        mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                        mBtnScanWuLiao.setEnabled(false);
                        mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
                        mBtnChongXinRuKu.setEnabled(true);
                    } else if (result.contains("3@")) {
                        String[] str = result.split("@");

                        if (str.length > 0) {
                            mSharedPreferencesHelper.putString(RU_KU_QING_ZHANG_MID, mid);
                            mResultCode = str[0];
                            et_num.setText(str[1]);
                            num = Double.parseDouble(str[1]);
                            mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                            mBtnScanWuLiao.setEnabled(false);
                            mLlCangWei.setVisibility(View.VISIBLE);

                            et_num.addTextChangedListener(numTextWatcher);

                        }

                    } else if (result.equals("-1")) {
                        ArmsUtils.makeText(getContext(), "入库区不可用");
                    } else if (result.equals("0")) {
                        ArmsUtils.makeText(getContext(), "操作不合法");
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        }, getAapiUserName(), getApiPwd());
    }

    private void setInstoreBufferReadyForAGV(String bufferId, String spname) {
        showLoading();
        String methodName = "SetInstoreBufferReadyForAGV";
        SoapParams params = new SoapParams();
        params.put("bufferId", bufferId);
        params.put("spname", spname);

        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    if (result.equals("false")) {

                        //ArmsUtils.makeText(this, "1.此缓冲位已占满 2.此缓冲位没有启用 3.此缓冲位是出库缓冲位(标签贴错)");
                        ArmsUtils.makeText(getContext(), "缓冲位置不可用，请联系管理员");

                    } else {
                        mBtnChongXinRuKu.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
                        mBtnChongXinRuKu.setEnabled(true);
                        btnSaoMiaoHuanChongWeiZhi.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                        btnSaoMiaoHuanChongWeiZhi.setEnabled(false);
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (!TextUtils.isEmpty(mSharedPreferencesHelper.getString(RU_KU_QING_ZHANG_MID))) {
            showConfirmMessageDialog();
        } else {
            finish();
        }
    }

    private void showConfirmMessageDialog() {
        new QMUIDialog.CheckBoxMessageDialogBuilder(this)
                .setTitle("确认退出?")
                .setMessage("已扫描物料,确认退出?")
                .setChecked(true)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        mSharedPreferencesHelper.putString(RU_KU_QING_ZHANG_MID, "");
                        killMyself();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }
}
